package com.ml.mall.utils;

import com.ml.mall.bean.res.admin.variant.vo.VariantQueryOneVO;
import com.ml.mall.bean.res.admin.variant.vo.VariantQueryTopVO;
import com.ml.mall.bean.res.admin.supplier.SupplierQueryPageRes;
import com.ml.mall.bean.res.admin.supplier.vo.SupplierQueryOneVO;
import com.ml.mall.bean.vo.DateValueVO;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Created by kyyang on 2021/6/8 22:03
 */
public class DateTransformUtils {


    public static  List<DateValueVO>  getCurDateMonthDataInit(String startTime, String endTime, Map<String,Double> map){
        Calendar instance = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = new Date();
        try {
            parse = sdf.parse(startTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        instance.setTime(parse);
        int d = DateUtils.getDateDiff(startTime, endTime);
        List<DateValueVO> list = new ArrayList<>(d);
        for(int i = 0;i<=d;i++){
            BigDecimal hj = BigDecimal.ZERO;
            String format = sdf.format(instance.getTime());
            if(map!=null){
                Double aDouble = map.get(format);
                if(aDouble!=null){
                    hj = BigDecimal.valueOf(aDouble);
                }
            }
            list.add(new DateValueVO(format,hj));
            instance.add(Calendar.DAY_OF_MONTH,1);
        }
        return list;
    }

    public static  List<VariantQueryTopVO>  getCurDateMonthDataInit(String startTime, String endTime, List<VariantQueryTopVO> listVO){
        Calendar instance = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = new Date();
        try {
            parse = sdf.parse(startTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        instance.setTime(parse);
        int d = DateUtils.getDateDiff(startTime, endTime);

        List<VariantQueryTopVO> list = new ArrayList<>(d);
        for(int i = 0;i<=d;i++){
            BigDecimal a = BigDecimal.ZERO;
            BigDecimal b = BigDecimal.ZERO;
            Long c = 0L;
            String format = sdf.format(instance.getTime());
            VariantQueryTopVO vo = new VariantQueryTopVO();
            if(listVO!=null&&listVO.size()>0){
                Map<String, VariantQueryTopVO> collect = listVO.stream().collect(Collectors.toMap(VariantQueryTopVO::getYmd, Function.identity()));
                if(collect.get(format)!=null){
                    VariantQueryTopVO  v =  collect.get(format);
                    a = v.getTotalAllmoney();
                    b = v.getTotalTruePay();
                    c = v.getTotalQuantity();
                }
            }
            vo.setYmd(format);
            vo.setTotalAllmoney(a);
            vo.setTotalTruePay(b);
            vo.setTotalQuantity(c);
            list.add(vo);
            instance.add(Calendar.DAY_OF_MONTH,1);
        }
        return list;
    }

    public static List<VariantQueryOneVO> getCurDateMonthDataInit2(String startTime, String endTime, List<VariantQueryOneVO> listVO) {
        Calendar instance = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = new Date();
        try {
            parse = sdf.parse(startTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        instance.setTime(parse);
        int d = DateUtils.getDateDiff(startTime, endTime);

        List<VariantQueryOneVO> list = new ArrayList<>(d);
        for(int i = 0;i<=d;i++){
            BigDecimal a = BigDecimal.ZERO;
            Long c = 0L;
            String format = sdf.format(instance.getTime());
            VariantQueryOneVO vo = new VariantQueryOneVO();
            if(listVO!=null&&listVO.size()>0){
                Map<String, VariantQueryOneVO> collect = listVO.stream().collect(Collectors.toMap(VariantQueryOneVO::getYmd, Function.identity()));
                if(collect.get(format)!=null){
                    VariantQueryOneVO  v =  collect.get(format);
                    a = v.getTotalSlaes();
                    c = v.getQuantity();
                }
            }
            vo.setYmd(format);
            vo.setTotalSlaes(a);
            vo.setQuantity(c);
            list.add(vo);
            instance.add(Calendar.DAY_OF_MONTH,1);
        }
        return list;
    }

    public static List<SupplierQueryOneVO> getCurDateMonthDataInit3(String startTime, String endTime, List<SupplierQueryOneVO> listVO) {
        Calendar instance = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = new Date();
        try {
            parse = sdf.parse(startTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        instance.setTime(parse);
        int d = DateUtils.getDateDiff(startTime, endTime);

        List<SupplierQueryOneVO> list = new ArrayList<>(d);
        for(int i = 0;i<=d;i++){
            BigDecimal a = BigDecimal.ZERO;
            Long c = 0L;
            String format = sdf.format(instance.getTime());
            SupplierQueryOneVO vo = new SupplierQueryOneVO();
            if(listVO!=null&&listVO.size()>0){
                Map<String, SupplierQueryOneVO> collect = listVO.stream().collect(Collectors.toMap(SupplierQueryOneVO::getYmd, Function.identity()));
                if(collect.get(format)!=null){
                    SupplierQueryOneVO  v =  collect.get(format);
                    a = v.getTotalSales();
                    c = v.getTotalPo();
                }
            }
            vo.setYmd(format);
            vo.setTotalSales(a);
            vo.setTotalPo(c);
            list.add(vo);
            instance.add(Calendar.DAY_OF_MONTH,1);
        }
        return list;
    }
}
